<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <link rel="shortcut icon" href="#"/>
    <link rel="stylesheet" href="../element-ui/element.css">
    <style>
        .login {
            background: url("${pageContext.request.contextPath}/cln/images/bg.jpg") no-repeat center center;
            width: 100%;
            height: 100%;
            background-size: 100% 100%;
            position: fixed;
        }

        .bg {
            background-color: white;
            width: 450px;
            height: 370px;
            position: absolute;
            top: 45%;
            left: 75%;
            transform: translate(-50%, -50%);
        }

        .tabs {
            width: 400px;
            height: 300px;
            margin: 0 auto;
            /*为了垂直居中*/
            position: absolute;
            top: 45%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .demo-ruleForm {
            margin-top: 20px;
        }

        .col_txt1 {
            text-align: left;
        }

        .header_fidx {
            position: relative;
        }

        .getCode-button {
            border-radius: .3125rem;
            text-align: center;
        }

        .verifyPhone-button {
            color: #999;
            border-radius: .3125rem;
            text-align: center;
            font-size: 1rem;
        }
    </style>
</head>
<body>
<script src="../../vue/vue.js"></script>
<script src="../element-ui/element.js"></script>
<script src="../../jquery/jquery-3.4.1.js"></script>
<div id="login">
    <template>
        <!--element的Tabs组件 选项卡功能-->
        <div class="login">
            <div class="bg">
                <el-tabs v-model="activeName" @tab-click="handleClick" class="tabs">
                    <el-tab-pane label="登录" name="first">
                        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                                 class="demo-ruleForm">
                            <el-form-item label="手机号" prop="phone">
                                <el-row type="flex" class="row-bg header_fidx" :span="24">
                                    <el-col :span="20" class="col_txt1">
                                        <a class="grid-content">
                                            <el-input v-model.trim="ruleForm.phone" type="text" placeholder="请输入手机号"
                                                      id="phone"
                                                      name="phone" clearable/>
                                            </el-input>
                                        </a>
                                    </el-col>
                                </el-row>
                                <%--<el-input v-model="ruleForm.phone" auto-complete="off"></el-input>--%>
                            </el-form-item>
                            <el-form-item label="验证码" prop="verify" v-if="loginflag == 'yzm'">
                                <el-row type="flex" class="row-bg header_fidx" :span="24">
                                    <el-col :span="11" class="col_txt1">
                                        <a class="grid-content">
                                            <el-input v-model.trim="ruleForm.verify" type="text" placeholder="请输入验证码"
                                                      name="verify"
                                                      clearable/>
                                        </a>
                                    </el-col>
                                    <el-col :span="7" class="col_txt1">
                                        <a class="grid-content">
                                            <el-button @click="getCode()" v-show="isgetCode" style="margin-left: 14.5px"
                                                       class="getCode-button">点击获取
                                            </el-button>
                                            <div v-show="!isgetCode" :class="{ active: isActive }"
                                                 class="verifyPhone-button">
                                                <el-row>
                                                    <el-button v-show="isActive" style="margin-left: 14.5px" @click="reacquire()" >重新获取
                                                    </el-button>
                                                    <el-button v-show="!isActive" style="margin-left: 14.5px" disabled>
                                                        {{count}}s
                                                    </el-button>
                                                </el-row>
                                            </div>
                                        </a>
                                    </el-col>
                                </el-row>
                            </el-form-item>
                            <el-form-item label="密码" prop="password" v-if="loginflag == 'password'">
                                <el-row type="flex" class="row-bg header_fidx" :span="24">
                                    <el-col :span="20" class="col_txt1">
                                        <a class="grid-content">
                                            <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"
                                                      clearable></el-input>
                                        </a>
                                    </el-col>
                                </el-row>
                            </el-form-item>
                            <el-form-item>
                                <%--javascript:void(0);不会刷新页面--%>
                                <a href="javascript:void(0);" @click="forget" style="margin-left: 80px">忘记密码?</a>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="success" @click="login" style="margin-left: 30px">登录</el-button>
                                <el-button @click="resetForm('ruleForm')">重置</el-button>
                            </el-form-item>
                        </el-form>
                    </el-tab-pane>
                    <el-tab-pane label="注册" name="second">
                        <div id="register">
                            <iframe src="./register.jsp" align="center" width="100%" height="400" frameborder="no"
                                    border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
                        </div>
                    </el-tab-pane>
                </el-tabs>
            </div>
        </div>
    </template>
</div>
</body>
<script>
    var Main = {
        name: 'Login',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'))
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass')
                    }
                    callback()
                }
            };
            return {
                activeName: 'first',
                isgetCode: true,
                isActive: false,
                count: 0,
                timer: null,
                TIME_COUNT: 60,//验证码倒计时60s
                ruleForm: {
                    phone: '',//手机号
                    password: '',
                    verify: "", //验证码
                },
                loginflag: 'password',
                rules: {
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        // 这个只能验证手机号
                        {
                            pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|17[1-9]|18[1-9])[0-9]{8}$/,
                            message: '请输入合法手机号',
                            trigger: 'blur'
                        }
                    ],
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'}
                    ],
                    verify: [
                        {required: true, message: '验证码不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            // 选项卡切换
            handleClick(tab, event) {
            },
            // 重置表单
            resetForm(formName) {
                this.$refs[formName].resetFields()
            },
            //忘记密码
            forget() {
                this.loginflag = 'yzm';
            },
            login() {
                if (this.loginflag == 'yzm') {
                    $.ajax({
                        type: 'post',
                        url: '/forum/user/login',
                        dataType: 'json',
                        data: {
                            userPhone: this.ruleForm.phone,
                            other2: this.ruleForm.verify,
                        },
                        success: function (data) {
                            console.log(data);
                            if (data.state == '200') {
                                window.location.href = "../../3_main/3_main.html";
                            } else {
                                alert(data.msg);
                            }
                        }
                    })
                }
                if (this.loginflag == 'password') {
                    $.ajax({
                        type: 'post',
                        url: '/forum/user/login',
                        dataType: 'json',
                        data: {
                            userPhone: this.ruleForm.phone,
                            userPassword: this.ruleForm.password,
                        },
                        success: function (data) {
                            console.log(data);
                            if (data.state === '200') {
                               /* window.location.href = "../../2-post/post.html";*/
                                window.location.href = "../../3_main/3_main.html";
                            } else {
                                alert(data.msg);
                            }
                        }
                    })
                }
            },
            //获取验证码
            getyzm() {
                var that = this;
                $.ajax({
                    type: 'post',
                    url: '/forum/user/loginyzm',
                    dataType: 'json',
                    data: {
                        userPhone: that.ruleForm.phone,
                    },
                    success: function (data) {
                        console.log(data);
                        if (data.state === '200') {
                            that.yzm = data.data
                        } else {
                            alert(data.msg);
                        }
                    }
                })
            },
            //计时器
            getCode() {
                this.isgetCode = false;
                if (!this.timer) {
                    this.count = this.TIME_COUNT;
                    this.isActive = false;
                    this.timer = setInterval(() => {
                        if (this.count > 0 && this.count <= this.TIME_COUNT) {
                            this.count--;
                        } else {
                            this.isActive = true;
                            clearInterval(this.timer);
                            this.timer = null;
                        }
                    }, 1000);
                }
                this.getyzm();
            },

            //重新获取验证码
            reacquire() {
                this.getCode(this.ruleForm);
            }
        }
    };
    var Ctor = Vue.extend(Main);
    new Ctor().$mount('#login');

    $('#cosn').attr("disabled","disabled");
</script>
</html>
